(function () {
    'use strict';
    var SettingService = angular.module('feibo.setting.service');
    SettingService.service("UserManageService", userManageService);
    SettingService.service("SchoolListService", schoolListService);
    SettingService.service("NoticeService", noticeService);
    SettingService.service("AuthorityService", authorityService);
    SettingService.service("PrivilegeService", privilegeService);
    SettingService.service("configureManageService", configureManageService);
    SettingService.service("ExportService", exportService);
    noticeService.$inject = userManageService.$inject = schoolListService.$inject = ["HttpService"];
    authorityService.$inject = exportService.$inject = ["CommonService"];


    function userManageService(HttpService) {
        return {

            getUserList: function (param) {
                return HttpService.get(ENDPOINTS.user.fetch, param);
            },
            getUserById: function (id) {
                return HttpService.get(ENDPOINTS.user.user + '/' + id);
            },
            getuserrole: function () {
                return {
                    1: 'Admin',
                    2: 'School Manager',
                    3: 'Teacher',
                    4: 'Student',
                    5: 'Teaching Research',
                    6: 'Teacher Admin',
                    7: 'Sales',
                    8: 'Customer Service',
                    9: 'Unsigned',
                    10: 'Student Manager',
                    11: 'Teacher QA',
                    12: 'Team Leader',
                    13: 'PC',
                    14: 'OM',
                    15: 'PM',
                    16: 'HR',
                    17: 'ACC',
                    18: 'SCH'
                };
            },
            addUser: function (data) {
                return HttpService.post(ENDPOINTS.user.fetch, data);
            },
            editUser: function (data, pk) {
                var pk = pk ? pk : data.id;
                var url = ENDPOINTS.user.update + '/' + pk;
                return HttpService.put(url, data);
            },
            delUser: function (pk) {
                var url = ENDPOINTS.user.update + '/' + pk;
                return HttpService.delete(url);
            },
            getuserinfo: function () {
                return HttpService.getpersoninfo(ENDPOINTS.other.personinfo.fetch);
            },
            valiEmail: function (pk, email) {

                // http: //localhost:8888/u/valid?email=763613923@qq.com&number=a73754b8f4b511e59b89c81f66eaf804&pk=1f46b2f6d639404280a2ba81e7754451
                var param = {
                    url: window.location.host + '/u/setting/valiemail?email={email}&number={number}&pk={pk}'
                    //url: ENDPOINTS.baseurl + '/u/setting/valiemail?email={email}&number={number}&pk={pk}'
                };
                return HttpService.get(ENDPOINTS.other.valid.fetch + '/' + pk + '/email/' + email, param);
            },
            /**
             * 激活验证邮箱
             * @param{
             *   opk	String（32）	用户唯一标志	Y
             *   email	String（128）	验证邮箱	Y
             *   code	String（32）	激活参数pk	Y
             * }
             */
            actionEmail: function (param) {
                return HttpService.post(ENDPOINTS.other.valid.email, param);
            },
            valiCellphoe: function (pk, cellphone, code) {
                return HttpService.post(ENDPOINTS.other.valid.fetch + '/' + pk + '/cellphone/' + cellphone, {
                    code: code
                });
            },
            getCellValiCode: function (pk, cellphone) {
                return HttpService.get(ENDPOINTS.other.valid.fetch + '/' + pk + '/cellphone/' + cellphone);
            },
            /**
             * 修改用户密码
             * @pk      用户主键
             * @newPwd  新密码
             */
            rePwd: function (pk, newPwd) {
                return HttpService.post(ENDPOINTS.user.user + '/' + pk + '/change_password', {
                    new_pwd: newPwd,
                    allow_empty: 'true'
                });
            }
        };
    }

    function schoolListService(HttpService) {
        return {
            getshoolList: function (param) {
                return HttpService.get(ENDPOINTS.school.fetch, param, true);
            },
            editSchool: function (params, pk) {
                var id = pk ? pk : params.pk;
                delete params.pk;
                var url = ENDPOINTS.school.update + "/" + id;
                return HttpService.put(url, params);
            },
            addSchool: function (data) {
                return HttpService.post(ENDPOINTS.school.add, data);
            },
            delSchool: function (pk) {
                var url = ENDPOINTS.school.update + "/" + pk;
                return HttpService.delete(url);
            },
            getSingleShool: function (schoolid) {
                return HttpService.get(ENDPOINTS.school.update + "/" + schoolid);
            },

        };
    }

    function noticeService(HttpService) {
        /**
         * 消息的参数组成
         * @param
         * name	        String（64）	中文名	Y
         * en_name	    String（64）	英文名	Y
         * show_type	Int	显示类别，枚举	Y
         * code_name	String（64）	别名，唯一	Y
         * site_msg	    Int	站内信，枚举	Y
         * short_msg	Int	短信，枚举	Y
         * email	    Int	邮件，枚举	Y
         * mobile	    Int	移动设备，枚举	Y
         * wechat	    Int	微信，枚举	Y
         */
        return {
            //获取用户消息列表
            getuserNoticeList: function (pk) {
                return HttpService.get(ENDPOINTS.notice.userNotice + "/" + pk);
            },
            //修改消息配置
            editNoticeConfig: function () {
                // return HttpService.put(url, params);
            }
        };
    }

    function authorityService(CommonService){
        return {
            fetchPermissions: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetchPermissions;
                return CommonService.get(queryConfig);
            },
            fetchPermission: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetchGroupPermission;
                return CommonService.get(queryConfig);
            },
            updatePermission: function(putConfig){
                putConfig.endpoint = ENDPOINTS.user.fetchGroupPermission;
                return CommonService.put(putConfig);
            },
            fetchGroups: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetchGroups;
                return CommonService.get(queryConfig);
            },
            updatePermissions: function(putConfig){
                putConfig.endpoint = ENDPOINTS.user.updatePermissions;
                return CommonService.put(putConfig);
            },
            fetchActuallyPermission: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetchActuallyPermission;
                return CommonService.get(queryConfig);
            }
        };
    }

    function privilegeService(CommonService){
        return {
            fetchAuthGroups: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetchAuthGroups;
                return CommonService.get(queryConfig);
            },
            delPrivilege: function(deleteConfig){
                deleteConfig.endpoint = ENDPOINTS.user.delPrivilege;
                return CommonService.delete(deleteConfig);
            },
            fetchUserGroups: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetchUserGroups;
                return CommonService.get(queryConfig);
            },
            fetchUsers: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.fetch;
                return CommonService.get(queryConfig);
            },
            updateAuthGroup: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.updateAuthGroup;
                return CommonService.put(queryConfig);
            },
            addAuthGroups: function(postConfig){
                postConfig.endpoint = ENDPOINTS.user.fetchAuthGroups;
                return CommonService.post(postConfig);
            },
            updateUserGroups: function(postConfig){
                postConfig.endpoint = ENDPOINTS.user.fetchUserGroups;
                return CommonService.post(postConfig);
            },
            fetchMembersInGroup: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.user.membersInGroup;
                return CommonService.get(queryConfig);
            }
        };
    }

    function configureManageService(CommonService){
        return {
            fetchConfigStudyCenter: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.configure.fetchStudyCenter;
                return CommonService.get(queryConfig);
            },
            addConfigStudyCenter: function(postConfig){
                postConfig.endpoint = ENDPOINTS.configure.fetchStudyCenter;
                return CommonService.post(postConfig);
            },
            updateConfigStudyCenter: function(putConfig){
                putConfig.endpoint = ENDPOINTS.configure.fetchStudyCenter;
                return CommonService.put(putConfig);
            },
        };
    }

    function exportService(CommonService){
        return {
            fetchExportList: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.export.fetch;
                return CommonService.get(queryConfig);
            },
            deleteExport: function(deleteConfig){
                deleteConfig.endpoint = ENDPOINTS.export.delete;
                return CommonService.delete(deleteConfig);
            },
            fetchReport: function(queryConfig){
                queryConfig.endpoint = ENDPOINTS.other.registrations;
                return CommonService.get(queryConfig);
            }
        };
    }
})();
